<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <form action="">
        账号<input type="text" name="" id="username"><br>
        密码<input type="password" name="" id="password"><br>
        确认密码<input type="password" name="" id="newPwd"><br>
        <button id="reg">提交</button>
    </form>


    <script>
       
        let reg = document.getElementById('reg');
        let username = document.getElementById('username');
        let password = document.getElementById('password');
        let newPwd = document.getElementById('newPwd');
        reg.onclick = function (event) {
            event.preventDefault();
            if (password.value == newPwd.value) {//密码验证正确
                let obj = {
                    name: username.value,
                    pwd: password.value
                }
                let users = JSON.parse(localStorage.getItem('users'));
                if (users) {//浏览器已经存储的有users数组
                    //判断数组当中是否已经有该用户
                    let result = users.some((item) => item.name == username.value);
                    if (result) {
                        alert('用户已存在')
                    } else {
                        users.push(obj);
                        localStorage.users = JSON.stringify(users);
                        alert('注册成功')
                        location.href="login.html";
                    }

                } else {//没有数据，就存入数组
                    localStorage.setItem('users', JSON.stringify([obj]))
                    alert('注册成功')
                    location.href="login.html";
                }

            } else {//两次密码不一致
                alert('两次密码不一致')
            }
            password.value = '';
            newPwd.value = '';
            username.value = '';


        }



    </script>



</body>

</html>